package com.bdqn.shiro.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bdqn.shiro.pojo.BasDict;
import com.bdqn.shiro.service.BasDictService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Controller
public class DictController {

    @Resource
    private BasDictService basDictService;

    @RequestMapping("dict/list")
    public String List(Model model, BasDict basDict, @RequestParam(defaultValue = "1")Long pageIndex){
        Page page=new Page();
        page.setCurrent(pageIndex);
        page.setRecords(basDictService.getList(page,basDict));
        model.addAttribute("page",page);
        return "dict/list";
    }

    @GetMapping("dict/edit")
    public String edit(Model model,Long dictId){
        model.addAttribute("dict",basDictService.getById(dictId));
        return "dict/edit";
    }

    @GetMapping("dict/add")
    public String toAdd(){

        return "dict/add";
    }

    @PostMapping("dict/save")
    public String save(BasDict basDict){
        basDictService.saveOrUpdate(basDict);
        return "redirect:/dict/list";
    }

    @ResponseBody
    @GetMapping("dict/del/{dictId}")
    public Map del(@PathVariable("dictId")Long dictId){
        Map map=new HashMap();
        if (basDictService.removeById(dictId)){
            map.put("delResult","true");
        }
        return map;
    }
}
